﻿using System.Collections.Generic;
using System.Linq;
using ELearningSystem.Core.Interface;
using ELearningSystem.DAO.DAO;
using ELearningSystem.Gateway.Entities;

namespace ELearningSystem.Core.Manager
{
    public class PracticeManager:IPracticeManager
    {
        public bool CheckIfSubmittedAnswersAreCorrect(PracticeDAO practice, List<AnswerDAO> answers, out string message)
        {
            List<AnswerDAO> answersFromPractice = practice.Answers;
            int correctCount = (answersFromPractice.Where(answer => answer.IsCorrect)).Count();

            bool isCorrect = false;
            message = "No answer is submitted";    

            if (answers != null)
            {
                foreach (AnswerDAO answer in answers)
                {
                    if (answer.IsCorrect)
                    {
                        AnswerDAO submittedAnswer = answersFromPractice.FirstOrDefault(a => a.Id == answer.Id);
                        if (!submittedAnswer.IsCorrect)
                        {
                            message = "Wrong Answer";
                            return false;
                        }
                    }
                }

                isCorrect = answers.Count(answer => answer.IsCorrect) == correctCount ? true : false;

                message = isCorrect ? "Right Answer!" : "Wrong Answer";
            }
            return isCorrect;
        }
    }
}
